/**
 * @author  guocailee
 */

export default {
  getItem: function (name) {
    let cookieName = encodeURIComponent(name) + '='
    let cookieStart = document.cookie.indexOf(cookieName)
    let cookieValue = null
    let cookieEnd
    if (cookieStart >= 0) {
      cookieEnd = document.cookie.indexOf(';', cookieStart)
      if (cookieEnd < 0) {
        cookieEnd = document.cookie.length
      }
      cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd))
    }
    return cookieValue
  },

  setItem: function (name, value, expires, path, domain, secure) {
    var cookieText = encodeURIComponent(name) + '=' + encodeURIComponent(value)
    if (expires instanceof Date) {
      cookieText += '; expires=' + expires.toGMTString()
    }
    if (path) {
      cookieText += '; path=' + path
    }
    if (domain) {
      cookieText += '; domain=' + domain
    }
    if (secure) {
      cookieText += '; secure'
    }
    document.cookie = cookieText
  },

  removeItem: function (name, path, domain, secure) {
    this.set(name, '', new Date(0), path, domain, secure)
  }
}
